<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>企业之家管理员列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${global_templets_skin}/systemstyle/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">登录名</label>
          <div class="layui-input-block">
            <input type="text" name="loginname" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">手机</label>
          <div class="layui-input-block">
            <input type="text" name="telphone" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">邮箱</label>
          <div class="layui-input-block">
            <input type="text" name="email" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">角色</label>
          <div class="layui-input-block">
            <select name="role">
              <option value="0">管理员</option>
              <option value="1">超级管理员</option>
              <option value="6">编辑</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
        <a class="layui-btn layuiadmin-btn-admin" lay-href="/admin/user/add_admin.do" lay-text="添加管理员">添加</a>
      </div>

      <table id="user-table-form" lay-filter="user-table-form"></table>

      <script type="text/html" id="user-status-checkboxTpl">
        <!-- 这里的 checked 的状态只是演示 -->
        <input type="checkbox" name="sex" lay-skin="switch" lay-filter="test-table-sexDemo"
               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.status == 0 ? 'checked' : '' }}>
      </script>

      <script type="text/html" id="sex-table-switchTpl">
        <!-- 这里的 checked 的状态只是演示 -->
        <input type="checkbox" name="sex" lay-skin="switch" lay-text="女|男" lay-filter="test-table-sexDemo"
               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.sxe == 1 ? 'checked' : '' }}>
      </script>

      <script type="text/html" id="user-table-setTpl">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-href="/admin/user/edit_admin.do?id={{ d.id }}" lay-text="编辑管理员"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        {{#  if(d.role == '超级管理员'){ }}
        <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } else { }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } }}
      </script>
    </div>
  </div>
</div>


<script src="${global_templets_skin}/systemstyle/layui/layui.js"></script>
<script>
    layui.config({
        base: '${global_templets_skin}/systemstyle/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form'], function(){
        var table = layui.table
            ,form = layui.form
            ,$ = layui.$;

        table.render({
            elem: '#user-table-form'
            ,url:'/admin/user/admin_listData.do'
            ,cellMinWidth: 80
            ,cols: [[
                {type:'numbers'}
                ,{type: 'checkbox'}
                ,{field:'id', title:'ID', width:100, align:'center', unresize: true, sort: true}
                ,{field:'userName', title:'登录名', minWidth:120}
                ,{field:'nickname', title:'昵称', minWidth:120}
                ,{field:'createTime', title:'注册时间', align:'center', unresize: true}
                ,{field:'sex', title:'性别', align:'center', width:85, templet: '#sex-table-switchTpl', unresize: true}
                ,{field:'status', title:'用户状态', align:'center', templet: '#user-status-checkboxTpl'}
                ,{fixed: 'right', align:'center',  toolbar: '#user-table-setTpl'}
            ]]
            ,page: true
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('user-table-form', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('user-table-form')
                    ,checkData = checkStatus.data; //得到选中的数据

                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    ,title: '敏感操作，请验证口令'
                }, function(value, index){
                    layer.close(index);

                    layer.confirm('确定删除吗？', function(index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('user-table-form');
                        layer.msg('已删除');
                    });
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加管理员'
                    ,content: 'adminform.html'
                    ,area: ['420px', '420px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-back-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            table.reload('LAY-user-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(user-table-form)', function(obj) {
            var data = obj.data;
            json = JSON.stringify(data);
            switch(obj.event) {
                case 'detail':
                    console.log("投诉详情");
                    var index = layer.open({
                        type: 2,
                        title: "投诉详情页面",
                        area: ['30%', '60%'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade: 0.4,
                        skin: 'layui-layer-rim',
                        content: ["/medicaladmin/complain/complainRead?id="+data.id, "no"],
                    });
                    break;
                case 'del':
                    var delIndex = layer.confirm('真的删除id为' + data.id + "的信息吗?", function(delIndex) {
                        $.ajax({
                            url: '/medicaladmin/complain/delete/'+data.id,
                            type: "post",
                            success: function(suc) {
                                if(suc.code == 200) {
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(delIndex);
                                    console.log(delIndex);
                                    layer.msg("删除成功", {
                                        icon: 1
                                    });
                                } else {
                                    layer.msg("删除失败", {
                                        icon: 5
                                    });
                                }
                            }
                        });
                        layer.close(delIndex);
                    });
                    break;
            }
        });
    });
</script>
</body>
</html>